package com.njust.diary.Controlller;
import com.njust.diary.po.Note;
import com.njust.diary.po.User;
import com.njust.diary.service.NoteService;
import com.njust.diary.util.Page;
import com.njust.diary.vo.NoteVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/index")
public class IndexServlet extends HttpServlet{
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setAttribute("menupage","index");

        String actionName=req.getParameter("actionName");
        if("searchTitle".equals(actionName)){//条件查询
            String title =req.getParameter("title");
            req.setAttribute("title",title);

            noteList(req,resp,title,null,null);
        }else if("searchDate".equals(actionName)){//日期查询
            String date =req.getParameter("date");
            req.setAttribute("date",date);
         noteList(req,resp,null,date,null);
        }else if("searchType".equals(actionName)){//日期查询
            String type =req.getParameter("type");
            req.setAttribute("type",type);
            noteList(req,resp,null,null,type);
        }


        else{
            noteList(req,resp,null,null,null);//全部查询
        }




        req.setAttribute("changePage","note/list.jsp");
  req.getRequestDispatcher("index.jsp").forward(req,resp);




    }

    private void noteList(HttpServletRequest req, HttpServletResponse resp,String title,String date,String type) {
    String pageNum =req.getParameter("pageNum");

    User user =(User) req.getSession().getAttribute("user");

    Page<Note> page =new NoteService().findNoteListByPage(pageNum,user.getUserId(),title,date,type);

    req.setAttribute("page",page);

    List<NoteVo> dateInfo =new NoteService().findNoteCountByDate(user.getUserId());

   req.getSession().setAttribute("dateInfo",dateInfo);
        List<NoteVo> typeInfo =new NoteService().findNoteCountByType(user.getUserId());

        req.getSession().setAttribute("typeInfo",typeInfo);

    }


}
